<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

/**
 * App\Models\UserMoney
 *
 * @property int $id
 * @property int $user_id 用户ID
 * @property int $type 类型:1=收入,2=支出
 * @property int $pre_money 变更前余额
 * @property int $money 变更余额
 * @property int $after_money 变更后余额
 * @property string|null $remark 备注
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|UserMoney newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|UserMoney newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|UserMoney query()
 * @method static \Illuminate\Database\Eloquent\Builder|UserMoney whereAfterMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserMoney whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserMoney whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserMoney whereMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserMoney wherePreMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserMoney whereRemark($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserMoney whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserMoney whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserMoney whereUserId($value)
 * @mixin \Eloquent
 */
class UserMoney extends Model
{
	use HasDateTimeFormatter;
    protected $table = 'user_moneys';

    protected $fillable = [
        'user_id','type','pre_money','money','after_money','remark'
    ];


    /**
     * 用户收支记录
     * @param int $type 类型:1=收入,2=支出
     * @param int $user_id 用户ID
     * @param float|int $money 变更余额
     * @param string $remark 备注说明
     * @return void
     */
    public static function money(int $type, int $user_id, float|int $money, string $remark = '')
    {
        if ($money != 0) {
            DB::beginTransaction();
            try {
                $user = User::query()->where("id",$user_id)->first();
                $pre_money = $user->money?:0;
                $after_money = $user->money + $money;
                $res = self::create([
                    'type' => $type, 'user_id' => $user_id, 'pre_money' => $pre_money, 'money' => $money,
                    'after_money' => $after_money, 'remark' => $remark
                ]);
                if ($res){
                    User::query()->where("id",$user_id)->update(['money'=>$after_money]);
                }
                DB::commit();
            } catch (Exception $e) {
                Log::error($e->getMessage());
                DB::rollBack();
            }
        }
    }


    /**
     * 获取用户的
     * @param $user_id
     * @return array
     */
    public static function getList($user_id){
        $state = request()->input('state',0);
        $limit = request()->input('limit',10);
        $list = self::whereUserId($user_id)
            ->when($state,function ($query,$value){
                $query->where("money",$value==1?">":"<",0);
            })
            ->paginate($limit);
        $data['items'] = $list->items();
        $data['lastPage'] = $list->lastPage();
        $data['total'] = $list->total();
        return $data;
    }

}
